import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
import { IPCCommnd, FileType } from './const';
import { Task } from './biz/ipc/feature.const';

const electronHandler = {
  ipcRenderer: {
    chooseFiles: (fileType: FileType) =>
      ipcRenderer.invoke(IPCCommnd.ChooseFiles, fileType),
    importCertificate: (filePath: string) =>
      ipcRenderer.invoke(IPCCommnd.ImportCertificate, filePath),
    verifyCertificate: () => ipcRenderer.invoke(IPCCommnd.VerifyCertificate),
    chooseSingleFile: (fileType: FileType) =>
      ipcRenderer.invoke(IPCCommnd.ChooseSingleFile, fileType),
    chooseDir: () => ipcRenderer.invoke(IPCCommnd.ChooseDir),
    getColumn: (filename: string) =>
      ipcRenderer.invoke(IPCCommnd.GetColumn, filename),
    showError: (title: string, content: string) =>
      ipcRenderer.invoke(IPCCommnd.ShowError, title, content),
    showMessage: (title: string, content: string) =>
      ipcRenderer.invoke(IPCCommnd.ShowMessage, title, content),
    doMerge: (
      filename1: string,
      filename2: string,
      exportPath: string,
      twoCols: string[],
      exportCols: string[]
    ) =>
      ipcRenderer.invoke(
        IPCCommnd.DoMerge,
        filename1,
        filename2,
        exportPath,
        twoCols,
        exportCols
      ),
    verticalMerge: (task: Task, scanDir: string, exportDir: string) =>
      ipcRenderer.invoke(IPCCommnd.VerticalMerge, task, scanDir, exportDir),
    csvToExcel: (task: Task, scanDir: string, exportDir: string) =>
      ipcRenderer.invoke(IPCCommnd.CsvToExcel, task, scanDir, exportDir),
    mainLog: (level: string, ...params: any[]) => {
      ipcRenderer.invoke(IPCCommnd.MainLog, level, params);
    },
    exportLog: (exportDir: string) => {
      ipcRenderer.invoke(IPCCommnd.MainExportLog, exportDir);
    },
    receiveProgress: (
      callback: (event: IpcRendererEvent, ...args: any[]) => void
    ) => ipcRenderer.on(IPCCommnd.MainSendProgress, callback),
  },
};
contextBridge.exposeInMainWorld('electronAPI', electronHandler);
export type ElectronHandler = typeof electronHandler;
